//
//  MKAPIHttpBaseTestCase.m
//  MKAPITests
//
//  Created by 苏尚进 on 2022/7/29.
//  Copyright © 2022 moon. All rights reserved.
//

#import "MKAPIHttpBaseTestCase.h"
#import <MKAPI/MKAPI.h>

@implementation MKAPIHttpBaseTestCase

//- (void)test_reqbase{
//    MKAPIHttpRequestBase *req = [[MKAPIHttpRequestBase alloc] init];
//    req.headersParam
//        .P(@"ua", @"ios")
//        .P(@"flag", nil)
//    ;
//    req.keyValuesParam
//        .P(@"name", @"abc")
//        .P(@"name", nil)
//        .P(@"list", @[@1,@2])
//        .P(@"dict", @{@"a":@0,@"b":@YES})
//    ;
//    NSLog(@"req:%@",req);
//}
//- (void)test_MKAPIHttpHeadersParam{
//    MKAPIHttpRequestBase *req = [[MKAPIHttpRequestBase alloc] init];
//    req.url = @"httsp://a.b.com";
//    req.headersParam
//        .P(@"ua", @"ios safaria")
//        .P(@"flag", @"true")
//        .P(@"flag", nil)
//    ;
//    req.keyValuesParam
//        .P(@"name", @"abc")
//        .P(@"obj", [NSObject new])
//        .P(@"flag", @(YES))
//        .P(@"count", @10)
//        .P(@"date", [NSDate date])
//        .P(@"out", @"out")
//        .P(@"out", nil)
//        .P(@"array", @[@1,@2])
//        .P(@"dict", @{@"a":@"1",@"b":@"2"})
//        .P(@"set", [NSSet setWithArray:@[@1,@2]])
//    ;
//    [req addCompletionHandler:^(__kindof MKAPITask * _Nonnull task, BOOL success, NSError * _Nullable error) {
//        
//    }];
//    [req run];
//    
////    req.whenCompletion = ^(__kindof MKAPIHttpRequestBase * _Nonnull request, BOOL success, NSError * _Nullable error) {
////        
////    };
////    [req send];
//    
//    XCTAssertTrue(([req.headersParam.dictionary isEqualToDictionary:@{@"ua":@"ios safaria"}]),@"headersParam fail");
//    NSLog(@"query:%@",req.keyValuesParam.keyValuesUrlEncodeString);
//    NSLog(@"dictionaryParam:%@",req.keyValuesParam.dictionary);
//}

- (void)test_NSDictionary{
    {
        NSDictionary *dict = @{};
        XCTAssertTrue([dict mkapi_objectForKeyPath:@"0"]==nil,@"@{}.0=nil");
        XCTAssertTrue([dict mkapi_objectForKeyPath:@"0.k"]==nil,@"@{}.0.k=nil");
        XCTAssertTrue([dict mkapi_objectForKeyPath:@"k"]==nil,@"@{}.k=nil");
        XCTAssertTrue([dict mkapi_objectForKeyPath:@"k.0"]==nil,@"@{}.k.0=nil");
    }
    {
        NSDictionary *dict = @{
            @"a":@"a1",
            @"b":@[@1,@2,@3,@[@"v1",@"v2"]],
            @"c":@{@"k1":@"v1",@"k2":@[@1]},
            @"d":[[NSURL alloc] initWithString:@"https://a.b.com"],
        };
        XCTAssertTrue([dict mkapi_objectForKeyPath:@"0"]==nil,@"@{}.0=nil");
        XCTAssertTrue([dict mkapi_objectForKeyPath:@"0.k"]==nil,"@{}.0.k=nil");
        
        XCTAssertTrue([[dict mkapi_objectForKeyPath:@"a"] isEqual:@"a1"],@"@{}.a=a1");
        XCTAssertTrue([dict mkapi_objectForKeyPath:@"a.nil"]==nil,@"@{}.nil=nil");
        XCTAssertTrue([dict mkapi_objectForKeyPath:@"a.0"]==nil,@"@{}.0=nil");
        
        XCTAssertTrue([[dict mkapi_objectForKeyPath:@"b.0"] isEqual:@1],@"@{}.b.0=1");
        XCTAssertTrue([dict mkapi_objectForKeyPath:@"b.0.0"]==nil,@"@{}.b.0.0=nil");
        XCTAssertTrue([[dict mkapi_objectForKeyPath:@"b.3"] isEqual:(@[@"v1",@"v2"])],@"@{}.b.3!=nil");
        XCTAssertTrue([[dict mkapi_objectForKeyPath:@"b.3.1"] isEqual:@"v2"],@"@{}.b.3.1=v2");
        XCTAssertTrue([dict mkapi_objectForKeyPath:@"b.4"]==nil,@"@{}.b.4=nil");
        
        XCTAssertTrue([[dict mkapi_objectForKeyPath:@"c.k1"] isEqual:@"v1"],@"@{}.c.k1=v1");
        XCTAssertTrue([[dict mkapi_objectForKeyPath:@"c.k2.0"] isEqual:@1],@"@{}.c.k2.0=1");
        XCTAssertTrue([dict mkapi_objectForKeyPath:@"c.k2.a"]==nil,@"@{}.c.k2.a=nil");
        XCTAssertTrue([[dict mkapi_objectForKeyPath:@"d"] isKindOfClass:[NSURL class]],@"@{}.d=NSURL");
        XCTAssertTrue([dict mkapi_objectForKeyPath:@"d.host"]==nil,@"@{}.d.a=nil");
    }
}
- (void)test_NSMutableDictionary{
    NSDictionary *d0 = @{
        @"a":@"a1",
        @"b":@[@1,@2,@3],
        @"c":@{@"k1":@"v1",@"k2":@[@1,@{@"c1":@"v1"}]},
        @"d":[[NSURL alloc] initWithString:@"https://a.b.com"],
    };
    {
        NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithDictionary:d0];
        [dict mkapi_setObject:nil forKeyPath:@"ab"];
        XCTAssertTrue([dict mkapi_objectForKeyPath:@"aa"]==nil,@"@{}.ab=nil");
        
        [dict mkapi_setObject:nil forKeyPath:@"a"];
        XCTAssertTrue([dict mkapi_objectForKeyPath:@"a"]==nil,@"@{}.a=nil");
        [dict mkapi_setObject:@"a1" forKeyPath:@"a"];
        XCTAssertTrue([[dict mkapi_objectForKeyPath:@"a"] isEqual:@"a1"],@"@{}.a=a1");
        
        [dict mkapi_setObject:nil forKeyPath:@"b.1"];
        XCTAssertTrue([[dict mkapi_objectForKeyPath:@"b.1"] isEqual:@3],@"@{}.b.1=nil");
        [dict mkapi_setObject:@2 forKeyPath:@"b.1"];
        XCTAssertTrue([[dict mkapi_objectForKeyPath:@"b.1"] isEqual:@2],@"@{}.b.1=@2");
        [dict mkapi_setObject:@{@"k1":@"c1",@"k2":@"c2"} forKeyPath:@"b.0"];
        XCTAssertTrue([[dict mkapi_objectForKeyPath:@"b.0.k1"] isEqual:@"c1"],@"@{}.b.0.k1=c1");
        XCTAssertTrue([[dict mkapi_objectForKeyPath:@"b.0.k2"] isEqual:@"c2"],@"@{}.b.0.k2=c2");
        [dict mkapi_setObject:nil forKeyPath:@"b.1"];
        XCTAssertTrue([dict mkapi_objectForKeyPath:@"b.1"]==nil,@"@{}.b.1=nil");
        
        [dict mkapi_setObject:@"v0" forKeyPath:@"c.k2.0"];
        XCTAssertTrue([[dict mkapi_objectForKeyPath:@"c.k2.0"] isEqual:@"v0"],@"@{}.c.k2.0=v2");
        XCTAssertTrue(![[dict mkapi_objectForKeyPath:@"c.k2.1"] isEqual:@1],@"@{}.c.k2.1!=1");
        [dict mkapi_setObject:@"v2" forKeyPath:@"c.k2"];
        XCTAssertTrue([[dict mkapi_objectForKeyPath:@"c.k2"] isEqual:@"v2"],@"@{}.c.k2=v2");
        
        [dict mkapi_setObject:@"v1" forKeyPath:@"e.k1.kk1"];
        XCTAssertTrue([[dict mkapi_objectForKeyPath:@"e.k1.kk1"] isEqual:@"v1"],@"@{}.e.k1.kk1=v1");
        [dict mkapi_setObject:nil forKeyPath:@"e.k1.kk1"];
        XCTAssertTrue([dict mkapi_objectForKeyPath:@"e.k1.kk1"]==nil,@"@{}.e.k1.kk1=nil");
        
        [dict mkapi_setObject:@"v1" forKeyPath:@"f.0.kk1.0"];
        XCTAssertTrue([[dict mkapi_objectForKeyPath:@"f.0.kk1.0"] isEqual:@"v1"],@"@{}.f.0.kk1.0=v1");
        
        [dict mkapi_setObject:@"v1" forKeyPath:@"d.0"];
        XCTAssertTrue([[dict mkapi_objectForKeyPath:@"d.0"] isEqual:@"v1"],@"@{}.d.0=v1");
        
        [dict mkapi_setObject:[[NSURL alloc] initWithString:@"https://a.b.com"] forKeyPath:@"d"];
        [dict mkapi_setObject:@"v1" forKeyPath:@"d.d1"];
        XCTAssertTrue([[dict mkapi_objectForKeyPath:@"d.d1"] isEqual:@"v1"],@"@{}.d.d1=v1");
    }
}
@end
